package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.feign.wemedia.IWemediaClient;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 陈辉
 * @data 2023 15:18
 */
@Service
@Transactional
@Slf4j
public class HotArticleServiceImpl implements HotArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private IWemediaClient wemediaClient;

    @Autowired
    private CacheService cacheService;

    /**
     * 计算热点文章
     */
    @Override
    public void computeHotArticle() {
        //1. 查询前5的文章
        //计算得到5天前的此刻
        Date dateParam = DateTime.now().minusDays(5).toDate();
        List<ApArticle> apArticleList = apArticleMapper.findArticleListByLast5days(dateParam);

        //2. 给每篇计算分值
        List<HotArticleVo> hotArticleVoList = apArticleList.stream().map(article -> {
            HotArticleVo hotArticleVo = new HotArticleVo();
            BeanUtils.copyProperties(article, hotArticleVo);
            Integer score = computeScore(article);
            hotArticleVo.setScore(score);
            return hotArticleVo;
        }).collect(Collectors.toList());

        //3. 为每个频道缓存30条热点文章
        List<Integer> channelIds = apArticleList.stream().map(ApArticle::getChannelId).distinct().collect(Collectors.toList());
        for (Integer channelId : channelIds) {
            //对文章按渠道进行分类
            List<HotArticleVo> channelArticleVoList = hotArticleVoList.stream().filter(article -> article.getChannelId().equals(channelId))
                    .collect(Collectors.toList());

            //按分数进行排序：降序
            //排序后只要前30条
            List<HotArticleVo> hotArticleVos = channelArticleVoList.stream().sorted((o1, o2) -> o2.getScore() - o1.getScore()).limit(30).collect(Collectors.toList());

            //将处理好的热点文章存入缓存
            cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + channelId, JSON.toJSONString(hotArticleVos));
        }


        //单独处理“推荐”频道
        //按分数进行排序：降序
        //排序后只要前30条
        List<HotArticleVo> hotArticleVos = hotArticleVoList.stream().sorted((o1, o2) -> o2.getScore() - o1.getScore()).limit(30).collect(Collectors.toList());

        //将处理好的热点文章存入缓存
        cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + ArticleConstants.DEFAULT_TAG, JSON.toJSONString(hotArticleVos));
    }


    /**
     * 计算文章的具体分值
     *
     * @param apArticle
     * @return
     */
    private Integer computeScore(ApArticle apArticle) {
        Integer scere = 0;
        if (apArticle.getLikes() != null) {
            scere += apArticle.getLikes() * ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
        }
        if (apArticle.getViews() != null) {
            scere += apArticle.getViews();
        }
        if (apArticle.getComment() != null) {
            scere += apArticle.getComment() * ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
        }
        if (apArticle.getCollection() != null) {
            scere += apArticle.getCollection() * ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
        }

        return scere;
    }
}
